package tianrun.ziguan.api.calculate.config.controller.common;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tianrun.ziguan.api.calculate.config.dto.request.common.add.edit.AddOrEditBody;
import tianrun.ziguan.api.calculate.config.dto.request.common.CopyBody;
import tianrun.ziguan.api.calculate.config.dto.request.common.DeleteBody;
import tianrun.ziguan.api.calculate.config.dto.request.common.InfoBody;
import tianrun.ziguan.api.calculate.config.dto.request.common.add.edit.AddOrEditBodyBatch;
import tianrun.ziguan.api.calculate.config.dto.request.common.list.ListBody;
import tianrun.ziguan.api.calculate.config.dto.request.common.list.PageBody;
import tianrun.ziguan.api.calculate.config.service.common.CommonService;
import tianrun.ziguan.api.calculate.config.utils.HttpResponseTools;
import tianrun.ziguan.api.common.core.base.ResultBody;
import tianrun.ziguan.api.common.core.base.ResultPageBody;
import tianrun.ziguan.api.common.core.base.ReturnMessage;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@RestController
@RequestMapping("common")
@Api(tags = "通用接口")
public class CommonController {

    private final CommonService commonService;

    public CommonController(CommonService commonService) {
        this.commonService = commonService;
    }

    @ApiOperation(value = "新增或修改")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("addOrEdit")
    public ResponseEntity<ResultBody<?>> addOrEdit(@Valid @RequestBody AddOrEditBody body) {
        return HttpResponseTools.good(ResultBody.of(commonService.addOrEdit(body)));
    }
    @ApiOperation(value = "新增或修改(批量)")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("addOrEditByBatch")
    public ResponseEntity<ResultBody<?>> addOrEditBatch(@Valid @RequestBody AddOrEditBodyBatch body) {
        return HttpResponseTools.good(ResultBody.of(commonService.addOrEditBatch(body)));
    }

    @ApiOperation(value = "删除")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("delete")
    public ResponseEntity<ReturnMessage> delete(@Valid @RequestBody DeleteBody body) {
        commonService.delete(body);
        return HttpResponseTools.good();
    }

    @ApiOperation(value = "列表")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("list")
    public ResponseEntity<ResultBody<List<Map<String, Object>>>> list(@Valid @RequestBody ListBody body) {
        return HttpResponseTools.good(ResultBody.of(commonService.list(body)));
    }

    @ApiOperation(value = "分页")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("page")
    public ResponseEntity<ResultPageBody<Map<String, Object>>> page(@Valid @RequestBody PageBody body) {
        return HttpResponseTools.good(ResultPageBody.of(commonService.page(body), Function.identity()));
    }

    @ApiOperation(value = "详情")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("info")
    public ResponseEntity<ResultBody<Map<String, Object>>> info(@Valid @RequestBody InfoBody body) {
        return HttpResponseTools.good(ResultBody.of(commonService.info(body)));
    }

    @ApiOperation(value = "复制")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("copy")
    public ResponseEntity<ReturnMessage> copy(@Valid @RequestBody CopyBody body) {
        commonService.copy(body);
        return HttpResponseTools.good();
    }
}
